package com.daohang361.imqq.decoder;

import com.daohang361.imqq.Asycore.QQUser;
import com.daohang361.imqq.tool.Hex2Bytes;
import com.daohang361.imqq.tool.MyUtil;


public class DecoderEB extends AbstractDecoder {
	@Override
	public QQUser decode() throws Exception {
		myArray.decryptRemain(sessionQQ.getAttr("SessionKey"));
		decryptAll=myArray.extractAll();
		System.out.println(Integer.toHexString(myArray.getCommand())+":"+Hex2Bytes.bytesToHexString(decryptAll));
		int friendTotalPage=MyUtil.byte2Int(decryptAll[15]);//总片段
		int firendCurrentPage=MyUtil.byte2Int(decryptAll[17]);//当前片段
	System.out.println("EB总页数:"+friendTotalPage+"当前第"+firendCurrentPage+"页.");
		sessionQQ.addAttr("friendTotalPage",new byte[]{ decryptAll[15]});
		sessionQQ.addAttr("firendCurrentPage",new byte[]{ decryptAll[17]});
		addFriends(decryptAll);
		if(firendCurrentPage<friendTotalPage)
		{
			sessionQQ.setLogintimes(sessionQQ.getLogintimes()-1);//如果不是最后一次为登陆包不前进
		}
			
		sessionQQ.setLogintimes(sessionQQ.getLogintimes()+1);//登陆包前进
		sessionQQ.setPakage(sessionQQ.getPakage()+1);//登陆包自动增长
		return sessionQQ;
	}
	
	private void addFriends(byte[] decryptAll)
	{
		byte[] firendsAll = new byte[decryptAll.length-20];//去掉前面0100000000fa000002ab 4d93596c 00020001 0096 这些信息
		System.arraycopy(decryptAll, 20, firendsAll, 0, firendsAll.length);
		System.out.println("firendsAll.length:"+firendsAll.length/6);
		for(int i=0;i<firendsAll.length;)
		{
			byte[] tempQQ = new byte[4];//好友号码
			byte[] tempIndex = new byte[2];//好友群组归属索引
			System.arraycopy(firendsAll, i, tempQQ, 0, 4);
			System.arraycopy(firendsAll, i+4, tempIndex, 0, 2);
			int firendsQQ=MyUtil.bytes2Integer(tempQQ);
			//int index =MyUtil.bytes2Integer(tempIndex);
			int index =MyUtil.byte2Int(tempIndex[1])/4;
			sessionQQ.addFriend(index, firendsQQ);
			System.out.println("索引:int "+index+"好友号码:"+firendsQQ);
			i+=6;
		}
		
	}
}
